--require "MainScene"

JLPYLayer={}
JLPYLayerFunc={}
JLPYLayerFunc.__index = JLPYLayerFunc
setmetatable(JLPYLayer, JLPYLayerFunc)

JLPYLayer.NodeID = {HeroTable=1001}
local HeroItemID = {ImgBK=2001, TongShuai=2002, ZhanShu=2003, BaoPo=2004, JueZhao=2005, Name=2006, Button=3001}

JLPYLayerFunc.getNode = function(self, gid)
  local l = {}
  setmetatable(l, JLPYLayerFunc)
  l.node = CCBReader:load("JunXiaoScene1.ccbi")
  print("JLPYLayer.getNode", JLPYLayer.node)
  l:init(gid) 
  return l 
end

JLPYLayerFunc.getScene = function(self)
  local sc = CCScene:create()
  sc:addChild(JLPYLayer:getNode().node)  
  print("JLPYLayer.getScene", sc)
  return sc
end

JLPYLayerFunc.init = function(self, gid)
  --self:initTable()
  setObjectExtTable(self.node:getChildByTag(1020), self)
  setObjectExtTable(self.node:getChildByTag(1021), self)
  setObjectExtTable(self.node:getChildByTag(1022), self)
  setObjectExtTable(self.node:getChildByTag(1111), self)

  self.currentHeroIndex = gid
  local menu = self.node:getChildByTag(1002)
  self.radioMenu = RadioMenu:create(menu, "menuItemChanged", self)
  self.radioMenu:setMenuState(4)
  self:setHeroLabel()
  self:setButton() 
end

JLPYLayerFunc.setButton = function(self)
  if self.trained==nil or self.trained==false then
    self.node:getChildByTag(1020):setVisible(false)
    self.node:getChildByTag(1021):setVisible(false)
    self.node:getChildByTag(1022):setVisible(true)
  else
    self.node:getChildByTag(1020):setVisible(true)
    self.node:getChildByTag(1021):setVisible(true)
    self.node:getChildByTag(1022):setVisible(false)
  end
end

JLPYLayerFunc.setHeroLabel = function(self)
  local layer = self.node:getChildByTag(8001)
  if self.trained==true then
    layer:setVisible(true)
  else
    layer:setVisible(false)
  end
  local hd = HeroData:create(self.currentHeroIndex)
  local sp  = addSpriteFromBK(self.node, 1001, "1.png")
  
  TypeCast(self.node:getChildByTag(2001), "CCLabelTTF"):setString(hd.heroConfig.name)
  TypeCast(self.node:getChildByTag(2001), "CCLabelTTF"):setColor(Equip.EquipColor[hd.heroConfig.quality])
  TypeCast(self.node:getChildByTag(2002), "CCLabelTTF"):setString(string.format("%d", hd.heroInfo.l))
  TypeCast(self.node:getChildByTag(2003), "CCLabelTTF"):setString(hd.heroConfig.desc)

  TypeCast(self.node:getChildByTag(2004), "CCLabelTTF"):setString(string.format("%d+%d", hd.heroConfig.initCommander, hd.heroInfo.c))
  TypeCast(self.node:getChildByTag(2005), "CCLabelTTF"):setString(string.format("%d+%d", hd.heroConfig.initTactics, hd.heroInfo.t))
  TypeCast(self.node:getChildByTag(2006), "CCLabelTTF"):setString(string.format("%d+%d", hd.heroConfig.initBlast, hd.heroInfo.b))
end

JLPYLayerFunc.setNewProperty = function(self, res)
  local hd = HeroData:create(self.currentHeroIndex)
  local layer = self.node:getChildByTag(8001)
  if res.data.foster.c>0 then
    TypeCast(layer:getChildByTag(2007), "CCLabelTTF"):setColor(ccc3(80, 255, 0))
    setSpriteImg(TypeCast(layer:getChildByTag(3001), "CCSprite"), "arrowup.png")
  else
    TypeCast(layer:getChildByTag(2007), "CCLabelTTF"):setColor(ccc3(255, 80, 0))
    setSpriteImg(TypeCast(layer:getChildByTag(3001), "CCSprite"), "arrowdown.png")
  end
  if res.data.foster.t>0 then
    TypeCast(layer:getChildByTag(2008), "CCLabelTTF"):setColor(ccc3(80, 255, 0))
    setSpriteImg(TypeCast(layer:getChildByTag(3002), "CCSprite"), "arrowup.png")
  else
    TypeCast(layer:getChildByTag(2008), "CCLabelTTF"):setColor(ccc3(255, 80, 0))
    setSpriteImg(TypeCast(layer:getChildByTag(3002), "CCSprite"), "arrowdown.png")
  end
  if res.data.foster.b>0 then
    TypeCast(layer:getChildByTag(2009), "CCLabelTTF"):setColor(ccc3(80, 255, 0))
    setSpriteImg(TypeCast(layer:getChildByTag(3003), "CCSprite"), "arrowup.png")
  else
    TypeCast(layer:getChildByTag(2009), "CCLabelTTF"):setColor(ccc3(255, 80, 0))
    setSpriteImg(TypeCast(layer:getChildByTag(3003), "CCSprite"), "arrowdown.png")
  end

  TypeCast(layer:getChildByTag(2007), "CCLabelTTF"):setString(string.format("%d+%d", hd.heroConfig.initCommander, hd.heroInfo.c+res.data.foster.c))
  TypeCast(layer:getChildByTag(2008), "CCLabelTTF"):setString(string.format("%d+%d", hd.heroConfig.initTactics, hd.heroInfo.t+res.data.foster.t))
  TypeCast(layer:getChildByTag(2009), "CCLabelTTF"):setString(string.format("%d+%d", hd.heroConfig.initBlast, hd.heroInfo.b+res.data.foster.b))
end

JLPYLayerFunc.menuItemChanged = function(self)

end
JLPYLayerFunc.onTrainButton = function(self, node, event)
  --local heros = UserData.userinfo.host.store.refresh.g
  --self.currentHeroIndex = node:getTag()
  NetworkRequest.trainGeneral1(self.currentHeroIndex, "onTrainComplete", self) 
end
JLPYLayerFunc.onTrainComplete = function(self, data)
  print("JLPYLayerFunc.onTrainComplete", data)
  local res = cjson.decode(data)
  if res.ret==0 then
    local hd = HeroData:create(self.currentHeroIndex)
    NetworkRequest.syncData(res)
    self.trained = true
    self:setHeroLabel()
    self:setNewProperty(res)
    self:setButton(res)
    --hd.heroInfo.c = hd.heroInfo.c+res.data.foster.c 
    --hd.heroInfo.t = hd.heroInfo.t+res.data.foster.t 
    --hd.heroInfo.b = hd.heroInfo.b+res.data.foster.b 
  else
    showMessage(res.msg, ccc3(255,255,0))
  end
end

JLPYLayerFunc.onSaveButton = function(self, node, event)
  NetworkRequest.saveTrainGeneral("onSaveComplete", self) 
end
JLPYLayerFunc.onSaveComplete = function(self, data)
  print("JLPYLayerFunc.onSaveComplete", data)
  local res = cjson.decode(data)
  if res.ret==0 then
    NetworkRequest.syncData(res)
    self.trained = false 
    self:setHeroLabel()
    --self:setNewProperty(res)
    self:setButton(res)
  else
    showMessage(res.msg, ccc3(255,255,0))
  end
end
JLPYLayerFunc.onCancelButton = function(self, node, event)
  NetworkRequest.cancelTrainGeneral("onCancelComplete", self) 
end
JLPYLayerFunc.onCancelComplete = function(self, data)
  print("JLPYLayerFunc.onCancelComplete", data)
  local res = cjson.decode(data)
  if res.ret==0 then
    --NetworkRequest.syncData(res)
    self.trained = false 
    self:setHeroLabel()
    self:setButton(res)
  else
    showMessage(res.msg, ccc3(255,255,0))
  end
end
JLPYLayerFunc.onCloseButton = function(self, node, event)
  --local layer = node:getParent():getParent()
  --setTouchEnabledAll(layer, true)
  --layer:removeChild(node:getParent(), true)
  MainScene.scene:removeChild(self.node, true)
  local l = JunXiaoScene:getNode()
  l.radioMenu:setMenuState(3)
  print("JLSJLayerFunc.onCloseButton", JLAllLayer.oldIndex)
  JLAllLayer.instance.uiStepper:setIndex(JLAllLayer.oldIndex)
  MainScene.scene:addChild(l.node)
end
